#import <objc/runtime.h>
#import "MRTabBarController.h"

@implementation UITabBar (MRNavigationController)

/*===================================================================
 说明:
	拦截UITabBar的动画，解决hidesBottomBarWhenPushed为YES时，右滑截图异常
 ==================================================================*/
-(id <CAAction>)actionForLayer:(CALayer *)layer forKey:(NSString *)event
{
	return (id <CAAction>)[NSNull null];
}

@end

@implementation UITabBarController (MRTabBarController)

- (void)dealloc
{
	[self viewWillDealloc];
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
	return UIInterfaceOrientationMaskAll;
}

@end

@implementation UITabBarItem (MRTabBarController)
@dynamic alpha, hidden, hidesLine, tintColor, barTintColor;

- (void)dealloc
{
	objc_removeAssociatedObjects(self);
}

//===================================================================
- (void)setHidden:(BOOL)hidden
{
	objc_setAssociatedObject(self, @"hidden", @(hidden), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (BOOL)hidden
{
	return [objc_getAssociatedObject(self, @"hidden") boolValue];
}

//===================================================================
- (void)setHidesLine:(BOOL)hidesLine
{
	objc_setAssociatedObject(self, @"hidesLine", @(hidesLine), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (BOOL)hidesLine
{
	return [objc_getAssociatedObject(self, @"hidesLine") boolValue];
}

//===================================================================
- (void)setAlpha:(CGFloat)alpha
{
	objc_setAssociatedObject(self, @"alpha", @(alpha), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
	[[UIApplication sharedApplication].keyWindow.rootViewController setNeedsStatusBarAppearanceUpdate];
}
- (CGFloat)alpha
{
	id obj = objc_getAssociatedObject(self, @"alpha");
	return obj ? [obj floatValue] : 1.f;
}

//===================================================================
- (void)setTintColor:(UIColor *)tintColor
{
	objc_setAssociatedObject(self, @"tintColor", tintColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (UIColor *)tintColor
{
	return objc_getAssociatedObject(self, @"tintColor");
}

//===================================================================
- (void)setBarTintColor:(UIColor *)barTintColor
{
	objc_setAssociatedObject(self, @"barTintColor", barTintColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (UIColor *)barTintColor
{
	return objc_getAssociatedObject(self, @"barTintColor");
}

@end

#pragma mark -
@implementation MRTabBarController

- (void)viewDidLoad
{
	[super viewDidLoad];
	self.delegate = self;
}

/*-(void)viewWillAppear:(BOOL)animated
{
	[super viewWillAppear:animated];
	[self.selectedViewController beginAppearanceTransition:YES animated: animated];
}

-(void)viewDidAppear:(BOOL)animated
{
	[super viewDidAppear:animated];
	[self.selectedViewController endAppearanceTransition];
}

-(void)viewWillDisappear:(BOOL)animated
{
	[super viewWillDisappear:animated];
	[self.selectedViewController beginAppearanceTransition:NO animated: animated];
}

-(void)viewDidDisappear:(BOOL)animated
{
	[super viewDidDisappear:animated];
	[self.selectedViewController endAppearanceTransition];
}*/

/*===================================================================
 说明:
    切换标签
 ==================================================================*/
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
	UINavigationController *nvc = (UINavigationController *)viewController;
	if ([nvc isKindOfClass:[UINavigationController class]] && nvc.viewControllers.count > 1)
		return YES;
	
	return (viewController != self.selectedViewController);
}

//- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
//{
//	[self setNeedsStatusBarAppearanceUpdate];
//}

@end
